//
//  EJSUserInfoViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/13.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSUserInfoViewController.h"
#import "DJShowSelectTool.h"
#import "EJSUpdateUserInfoRequest.h"
#import "EJSUploadAvatarRequest.h"
#import "EJSUserInfoRequest.h"
#import "EJSUpdateUserNickNameViewController.h"

@interface EJSUserInfoViewController ()<YDGUIImagePickerProviderDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *headIconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *sexLabel;
@property (weak, nonatomic) IBOutlet UILabel *ageLabel;

@property (strong, nonatomic) EJSUserModel *userModel;


@end

@implementation EJSUserInfoViewController

+ (instancetype)viewController{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    return [storyBoard instantiateViewControllerWithIdentifier:@"EJSUserInfoViewController"];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人资料";
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self refreshUserInfo];
    }];
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.userModel = [EJSUserModel userModel];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshUserInfo{
    EJSUserInfoRequest *req = [EJSUserInfoRequest new];
    req.hideLoadingView = YES;
    FlyWeakSelf
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.userModel = response.data;
        [self endRefresh];
    } failHandler:^(BaseResponse *response) {
        [self endRefresh];
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}

- (void)setUserModel:(EJSUserModel *)userModel{
    _userModel = userModel;
    [self.headIconImageView setImageWithUrl:_userModel.avatar PlaceHolder:[UIImage imageNamed:@"user_btn"]];
    self.nameLabel.text = _userModel.name;
    self.ageLabel.text = _userModel.age;
    self.sexLabel.text = _userModel.gender.integerValue == 1?@"男":@"女";
}

#pragma mark - TableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return [super tableView:tableView cellForRowAtIndexPath:indexPath];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 1){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *action =[UIAlertAction actionWithTitle:@"手机拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[YDGUIImagePickerProvider shareInstance] openCameraPhotoWithDelegate:self UniqueId:@""];
        } ];
        [alert addAction:action];
        action = [UIAlertAction actionWithTitle:@"选择相片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[YDGUIImagePickerProvider shareInstance] openLocalPhotoWithDelegate:self UniqueId:@""];
        } ];
        [alert addAction:action];
        
        action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }else if (indexPath.row == 2){
        EJSUpdateUserNickNameViewController *updateNameVC = [[EJSUpdateUserNickNameViewController alloc] initWithNibName:@"EJSUpdateUserNickNameViewController" bundle:nil];
        [self.navigationController pushViewController:updateNameVC animated:YES];
    }else if (indexPath.row == 3){
       
        FlyWeakSelf
        [DJShowSelectTool showSelectWithArr:@[@"男",@"女"] andDefaultStr:self.sexLabel.text complete:^(NSString *selectedStr, NSInteger num) {
            weakSelf.sexLabel.text = selectedStr;
            EJSUpdateUserInfoRequest *req = [EJSUpdateUserInfoRequest new];
            req.gender = @(num+1).stringValue;
            [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
                [self refreshUserInfo];
                [[HUDHelper sharedInstance] tipMessage:@"性别修改成功"];
            } failHandler:^(BaseResponse *response) {
                [[HUDHelper sharedInstance] tipMessage:response.message];
            }];
        }];
    }else if (indexPath.row == 4){
        NSMutableArray *ageArray = [[NSMutableArray alloc] init];
        for (int age = 1; age < 100; age ++) {
            [ageArray addObject:@(age).stringValue];
        }
        FlyWeakSelf
        [DJShowSelectTool showSelectWithArr:ageArray andDefaultStr:self.ageLabel.text complete:^(NSString *selectedStr, NSInteger num) {
            weakSelf.ageLabel.text = selectedStr;
            EJSUpdateUserInfoRequest *req = [EJSUpdateUserInfoRequest new];
            req.age = selectedStr;
            [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
                [self refreshUserInfo];
                [[HUDHelper sharedInstance] tipMessage:@"年龄修改成功"];
            } failHandler:^(BaseResponse *response) {
                [[HUDHelper sharedInstance] tipMessage:response.message];
            }];
        }];
    }
}


- (void)onChoosePhotoSuccessWithFilePath:(NSString *)filePath OrginImage:(UIImage *)orginImage EditImage:(UIImage *)editImage UniqueId:(NSString *)uinqueId{
    self.headIconImageView.image = editImage;
    
    EJSUploadAvatarRequest *req = [EJSUploadAvatarRequest new];
    req.avatar = @"avatar";
    [[HUDHelper sharedInstance] loading:@"头像上传中"];
    [req uploadWitImagedData:UIImageJPEGRepresentation(editImage, 1.0) uploadName:@"avatar" progress:^(NSProgress *progress) {
        
    } successHandler:^(BaseResponse *response) {
       [[HUDHelper sharedInstance] tipMessage:@"头像上传成功"];
        [self refreshUserInfo];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:@"头像上传失败"];
    }];
}
@end
